package com.wunong.smart.address.repository;

import com.wunong.smart.address.client.query.UserAddressQueryDTO;
import com.wunong.smart.address.domain.UserAddressDomain;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author create by zealot.zt
 */
public interface UserAddressRepository {

    UserAddressDomain getById(Long id);

    UserAddressDomain getById(Long userId, Long id);

    /**
     * 用户地址编号
     *
     * @param ids
     * @return
     */
    default Map<Long, UserAddressDomain> queryMapByIds(Collection<Long> ids) {
        List<UserAddressDomain> domains = queryByIds(ids);
        if (CollectionUtils.isEmpty(domains)) {
            return Collections.emptyMap();
        }

        return domains.stream().collect(Collectors.toMap(UserAddressDomain::getId, Function.identity()));
    }

    /**
     * 根据主键查询
     *
     * @param ids
     * @return
     */
    List<UserAddressDomain> queryByIds(Collection<Long> ids);

    default UserAddressDomain getByIdNonNull(Long userId, Long id) {
        UserAddressDomain domain = getById(userId, id);
        Objects.requireNonNull(domain, "亲，非法的用户地址");
        return domain;
    }

    UserAddressDomain getByIdDefaultQueryFirst(Long userId, Long id);

    Long save(UserAddressDomain domain);

    void update(UserAddressDomain domain);

    /**
     * 设为默认用户地址
     *
     * @param domain
     */
    void resetDefaultFalse(UserAddressDomain domain);

    /**
     * 设为默认用户地址
     *
     * @param domain
     */
    void setDefault(UserAddressDomain domain);

    /**
     * 根据用户id查询用户所有的地址
     *
     * @param userId
     * @return
     */
    List<UserAddressDomain> queryByUser(Long userId, Integer userRole);

    /**
     * 用户维度查询地址
     *
     * @param userId
     * @param query
     * @return
     */
    List<UserAddressDomain> query(Long userId, UserAddressQueryDTO query);

    /**
     * 根据用户id查询用户默认的地址信息
     *
     * @param userId
     * @return
     */
    UserAddressDomain getDefaultAddress(Long userId, Integer userRole);

}
